

// 订阅模型
let Dep = {
    clintList: {},
    //添加订阅
    listen: function (key, fn) {
        //短路表达式
        (this.clintList[key] || (this.clintList[key] = [])).push(fn)
    },
    //发布
    trigger: function () {
        // console.log(arguments);

        let key = Array.prototype.shift.call(arguments),
            fns = this.clintList[key];
        // console.log(key);
            
        // console.log(fns[0]);

        if (!fns || fns.length === 0) {
            console.log('返回false');
            return false
        }
        
        let fn = fns[0]
        for (let i = 0; fn; fn = fns[i++]) {
            console.log(arguments);
            fn.apply(this, arguments);
        }   

    }
}

// 数据劫持
let dataHi = function ({ data, tag, datakey, selector}) {
    let value = '',
        el = document.querySelector(selector);
        console.log(el);
        
    Object.defineProperty(data, datakey, {
        get: function () {
            console.log('取值');
            return value
        },
        set: function (val) {
            console.log('修改值');
            value = val
            //发布
            Dep.trigger(tag, val)
        }

    })
    // console.log(Dep.listen);
    
    //订阅
    Dep.listen(tag, function (text) {
        console.log('执行');
        
        el.innerHTML = text
    })
}
